﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Workflow.Runtime;
using BneyBaruch.Ingest.Core.ServiceModel;
using BneyBaruch.Ingest.MorningLesson.Services;
using BneyBaruch.Ingest.MorningLesson.Services.Data;
using System.ServiceModel;

namespace BneyBaruch.Ingest.MorningLesson.WorkflowCommunication.VideoCaptureBoxServer
{
    public class VideoCaptureBoxServerExchangeService : IVideoCaptureBoxServerExchangeService,IDisposable
    {
        #region Constants

        private const string FullVideoCaptureBoxServerEndpointConfigurationName = "FullVideoCaptureBoxServerEndpoint";
        private const string SplittedVideoCaptureBoxServerEndpointConfigurationName = "SplitedVideoCaptureBoxServerEndpoint";

        #endregion

        #region Fields

        private WorkflowRuntime workflowRuntime;
        private FullVideoCaptureBoxServerCallback fullVideoCaptureBoxServerCallback;
        private SplittedVideoCaptureBoxServerCallback splittedVideoCaptureBoxServerCallback;
        private DuplexServiceProxy<IFullVideoCaptureBoxServer> fullVideoCaptureBoxServer = null;
        private DuplexServiceProxy<ISplitedVideoCaptureBoxServer> splitedVideoCaptureBoxServer = null;

        #endregion

        #region Constructors

        public VideoCaptureBoxServerExchangeService(WorkflowRuntime workflowRuntime)
        {
            this.workflowRuntime = workflowRuntime;
            fullVideoCaptureBoxServerCallback = new FullVideoCaptureBoxServerCallback(this);
            splittedVideoCaptureBoxServerCallback = new SplittedVideoCaptureBoxServerCallback(this);

            fullVideoCaptureBoxServer = new DuplexServiceProxy<IFullVideoCaptureBoxServer>(
                new InstanceContext(fullVideoCaptureBoxServerCallback), // Instance context for callback duplex communication
                FullVideoCaptureBoxServerEndpointConfigurationName // Endpoint configuration name
            );

            splitedVideoCaptureBoxServer = new DuplexServiceProxy<ISplitedVideoCaptureBoxServer>(
                new InstanceContext(splittedVideoCaptureBoxServerCallback), // Instance context for callback duplex communication
                SplittedVideoCaptureBoxServerEndpointConfigurationName // Endpoint configuration name
            );
        }

        #endregion

        #region IVideoCaptureBoxServerExchangeService Events

        public event EventHandler<FileSystemPreparationReportEventArgs> FileSystemOnFullVideoCaptureBoxServerReadyToUse;
        public event EventHandler<FileSystemPreparationReportEventArgs> FileSystemOnSplittedVideoCaptureBoxServerReadyToUse;

        #endregion

        #region IVideoCaptureBoxServerExchangeService Prepare FileSystem Methods

        public void NotifyFileSystemBeforeRecordingCommandCompletedOnFullVideoCaptureBoxServer(Guid workflowInstanceId)
        {
            FileSystemPreparationReportEventArgs e = new FileSystemPreparationReportEventArgs(workflowInstanceId);

            if (FileSystemOnFullVideoCaptureBoxServerReadyToUse != null)
            {
                FileSystemOnFullVideoCaptureBoxServerReadyToUse(null, e);
            }
        }

        public void NotifyFileSystemBeforeRecordingCommandCompletedOnSplittedVideoCaptureBoxServer(Guid workflowInstanceId)
        {
            FileSystemPreparationReportEventArgs e = new FileSystemPreparationReportEventArgs(workflowInstanceId);

            if (FileSystemOnSplittedVideoCaptureBoxServerReadyToUse != null)
            {
                FileSystemOnSplittedVideoCaptureBoxServerReadyToUse(null, e);
            }
        }

        public void PrepareFileSystemBeforeRecordingOnFullVideoCaptureBoxServer(Guid workflowInstanceId)
        {
            fullVideoCaptureBoxServer.Proxy.PrepareFileSystemBeforeRecording(
                new PrepareFileSystemBeforeRecordingRequest()
                {
                    WorkflowInstanceId = workflowInstanceId
                }
            );
        }

        public void PrepareFileSystemBeforeRecordingOnSplittedVideoCaptureBoxServer(Guid workflowInstanceId)
        {
            splitedVideoCaptureBoxServer.Proxy.PrepareFileSystemBeforeRecording(
                new PrepareFileSystemBeforeRecordingRequest()
                {
                    WorkflowInstanceId = workflowInstanceId
                }
            );
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            if (fullVideoCaptureBoxServer != null)
                fullVideoCaptureBoxServer.Dispose();

            if (splitedVideoCaptureBoxServer != null)
                splitedVideoCaptureBoxServer.Dispose();
        }

        #endregion
    }
}
